package ec.semptac.jsf.control;

import ec.semptac.jsf.modelo.Capacitaciones;
import ec.semptac.jsf.facade.CapacitacionesFacade;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.annotation.PostConstruct;

@ManagedBean(name = "capacitacionesController")
@ViewScoped
public class CapacitacionesController extends AbstractController<Capacitaciones> {

    @EJB
    private CapacitacionesFacade ejbFacade;
    private UsuarioController idUsuarioController;
    private TipoCertificadoController idTipoCertificadoController;
    private TipoEventoController idTipoEventoController;
    private AreaCapacitacionController idAreaCapacitacionController;

    /**
     * Initialize the concrete Capacitaciones controller bean. The
     * AbstractController requires the EJB Facade object for most operations.
     * <p>
     * In addition, this controller also requires references to controllers for
     * parent entities in order to display their information from a context
     * menu.
     */
    @PostConstruct
    @Override
    public void init() {
        super.setFacade(ejbFacade);
        FacesContext context = FacesContext.getCurrentInstance();
        idUsuarioController = context.getApplication().evaluateExpressionGet(context, "#{usuarioController}", UsuarioController.class);
        idTipoCertificadoController = context.getApplication().evaluateExpressionGet(context, "#{tipoCertificadoController}", TipoCertificadoController.class);
        idTipoEventoController = context.getApplication().evaluateExpressionGet(context, "#{tipoEventoController}", TipoEventoController.class);
        idAreaCapacitacionController = context.getApplication().evaluateExpressionGet(context, "#{areaCapacitacionController}", AreaCapacitacionController.class);
    }

    public CapacitacionesController() {
        // Inform the Abstract parent controller of the concrete Capacitaciones?cap_first Entity
        super(Capacitaciones.class);
    }

    /**
     * Resets the "selected" attribute of any parent Entity controllers.
     */
    public void resetParents() {
        idUsuarioController.setSelected(null);
        idTipoCertificadoController.setSelected(null);
        idTipoEventoController.setSelected(null);
        idAreaCapacitacionController.setSelected(null);
    }

    /**
     * Sets the "selected" attribute of the Usuario controller in order to
     * display its data in a dialog. This is reusing existing the existing View
     * dialog.
     *
     * @param event Event object for the widget that triggered an action
     */
    public void prepareIdUsuario(ActionEvent event) {
        if (this.getSelected() != null && idUsuarioController.getSelected() == null) {
            idUsuarioController.setSelected(this.getSelected().getIdUsuario());
        }
    }

    /**
     * Sets the "selected" attribute of the TipoCertificado controller in order
     * to display its data in a dialog. This is reusing existing the existing
     * View dialog.
     *
     * @param event Event object for the widget that triggered an action
     */
    public void prepareIdTipoCertificado(ActionEvent event) {
        if (this.getSelected() != null && idTipoCertificadoController.getSelected() == null) {
            idTipoCertificadoController.setSelected(this.getSelected().getIdTipoCertificado());
        }
    }

    /**
     * Sets the "selected" attribute of the TipoEvento controller in order to
     * display its data in a dialog. This is reusing existing the existing View
     * dialog.
     *
     * @param event Event object for the widget that triggered an action
     */
    public void prepareIdTipoEvento(ActionEvent event) {
        if (this.getSelected() != null && idTipoEventoController.getSelected() == null) {
            idTipoEventoController.setSelected(this.getSelected().getIdTipoEvento());
        }
    }

    /**
     * Sets the "selected" attribute of the AreaCapacitacion controller in order
     * to display its data in a dialog. This is reusing existing the existing
     * View dialog.
     *
     * @param event Event object for the widget that triggered an action
     */
    public void prepareIdAreaCapacitacion(ActionEvent event) {
        if (this.getSelected() != null && idAreaCapacitacionController.getSelected() == null) {
            idAreaCapacitacionController.setSelected(this.getSelected().getIdAreaCapacitacion());
        }
    }
}
